package com.shuzi.MediaShare.gui.teleplay;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.base.BaseFrameAtivity;
import com.shuzi.MediaShare.gui.history.HistorUtil;
import com.shuzi.MediaShare.gui.movie.MovieDetailPlayerActivity;
import com.shuzi.MediaShare.gui.movie.MovieHomeActivity;
import com.shuzi.MediaShare.gui.movie.MovieHomeAdapter;
import com.shuzi.MediaShare.model.MovieInfo;
import com.shuzi.MediaShare.net.Protocol;
import com.shuzi.MediaShare.view.LoadMoreScrollListener;
import com.shuzi.MediaShare.view.LoadMoreScrollListener.setOnScrollToEndListener;
import com.shuzi.MediaShare.view.TypeChoiceLayout;

/**
 * 电视剧首页
 * 
 * @author lung 下午11:15:23 2013
 */
public class TeleplayHomeActivity extends BaseFrameAtivity
{

	/** 电视剧列表 */
	private List<MovieInfo> mediaInfos = null;

	/** 每次获取数据最少值 */
	private final String LIMIT = "15";

	/** 第一次获取数据 */
	private final int LOAD_DATA_FIRST = 0;

	/** 加载更多数据 */
	private final int LOAD_MORE_DATA = 1;

	/** 加载筛选数据 */
	private final int LOAD_CHOICE_DATA = 2;

	/** 显示列表 */
	private GridView gridView = null;

	/** 适配器 */
	private MovieHomeAdapter adapter = null;

	/** 每次加载的数据 */
	private List<MovieInfo> loadData = null;

	/** 是否在加载数据 */
	private boolean loadingData = false;

	/** 是否加载完数据 */
	private boolean loadDataEnd = false;

	/** 筛选 */
	private TypeChoiceLayout typeChoiceLayout;

	/** 地区 */
	public String selectAreaItem = "";

	/** 类型 */
	public String selectTypeItem = "";

	/** 时间 */
	public String selectTimeItem = "";

	/** 类型id */
	private String typeId = "0";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		typeId = getIntent().getStringExtra("TYPE_ID");
		gridView = (GridView) findViewById(R.id.movie_gridView);
		mediaInfos = new ArrayList<MovieInfo>();
		adapter = new MovieHomeAdapter(this, mediaInfos);
		gridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				if (mediaInfos != null && !mediaInfos.isEmpty())
				{
					Intent intent = new Intent();
					intent.setClass(TeleplayHomeActivity.this,
							MovieDetailPlayerActivity.class);
					intent.putExtra("MOVIEINFO", mediaInfos.get(position));
					HistorUtil.addToHistory(TeleplayHomeActivity.this, mediaInfos.get(position));
					startActivity(intent);
				}
			}
		});
		gridView.setOnScrollListener(new LoadMoreScrollListener(ImageLoader
				.getInstance(), true, true, new setOnScrollToEndListener()
		{

			@Override
			public void loadMoreWhenScrollToEnd()
			{
				loadMoreData();

			}

		}));
		typeChoiceLayout = (TypeChoiceLayout) findViewById(R.id.movie_choice_layout);
		typeChoiceLayout.setInfo(this, Constant.tMap.get(typeId));
		// 类型选择确定监听器
		typeChoiceLayout.okButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				typeChoiceLayout.setVisibility(View.GONE);
				selectAreaItem = typeChoiceLayout.getIndex(
						Constant.tMap.get(typeId).areaList,
						typeChoiceLayout.selectAreaItem);
				selectTypeItem = typeChoiceLayout.getIndex(
						Constant.tMap.get(typeId).typeList,
						typeChoiceLayout.selectTypeItem);
				selectTimeItem = typeChoiceLayout.selectTimeItem;
				loadData(LOAD_CHOICE_DATA, 0);
			}
		});
		titileBarLayout.typeButton.setBackgroundResource(R.drawable.teleplay_button);
		loadData(LOAD_DATA_FIRST, 0);
	}

	@Override
	protected boolean initData(Integer... params)
	{
		int type = params[0];
		boolean isSuccess = true;
		switch (type)
		{
		case LOAD_DATA_FIRST:
			// loadData = Protocol.queryDes("0", params[1] + "", LIMIT, "", "");
			loadData = Protocol.queryDes2(typeId, params[1] + "", LIMIT,
					selectAreaItem, selectTypeItem, selectTimeItem);
			if (loadData == null)
			{
				isSuccess = false;
			} else if (loadData.isEmpty())
			{
				isSuccess = true;
			}
			break;
		case LOAD_CHOICE_DATA:
			loadData = Protocol.queryDes2(typeId, params[1] + "", LIMIT,
					selectAreaItem, selectTypeItem, selectTimeItem);
			if (loadData == null)
			{
				isSuccess = false;
			} else if (loadData.isEmpty())
			{
				isSuccess = true;
			}
			break;
		case LOAD_MORE_DATA:
			if (loadData != null)
			{
				loadData.clear();
				loadData = null;
			}
			loadData = Protocol.queryDes2(typeId, params[1] + "", LIMIT,
					selectAreaItem, selectTypeItem, selectTimeItem);
			if (loadData == null)
			{
				isSuccess = false;
			} else if (loadData.isEmpty())
			{
				isSuccess = true;
			}
			break;

		}
		return isSuccess;
	}

	@Override
	protected void refreshView(boolean isSuccessful, Integer... params)
	{
		switch (params[0])
		{
		case LOAD_DATA_FIRST:
			if (isSuccessful)
			{
				mediaInfos.addAll(loadData);
				gridView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (loadData.size() < Integer.parseInt(LIMIT))
				{
					loadDataEnd = true;
				}
			} else
			{
				errorView.setVisibility(View.VISIBLE);
			}
			break;
		case LOAD_CHOICE_DATA:
			if (isSuccessful)
			{
				mediaInfos.clear();
				mediaInfos.addAll(loadData);
				gridView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (loadData.size() < Integer.parseInt(LIMIT))
				{
					loadDataEnd = true;
				} else
				{
					loadDataEnd = false;
				}
			} else
			{
				errorView.setVisibility(View.VISIBLE);
			}
			break;
		case LOAD_MORE_DATA:
			if (isSuccessful)
			{
				mediaInfos.addAll(loadData);
				adapter.notifyDataSetChanged();

				if (loadData.size() < Integer.parseInt(LIMIT))
				{
					loadDataEnd = true;
				}
			} else
			{

				Toast.makeText(TeleplayHomeActivity.this, "加载更多数据发生错误",
						Toast.LENGTH_SHORT).show();
			}
			loadingData = false;
			break;
		}

	}

	/**
	 * 加载更多数据
	 */
	private void loadMoreData()
	{
		if (mediaInfos != null && !loadingData && !loadDataEnd)
		{
			loadData(LOAD_MORE_DATA, mediaInfos.size());
			loadingData = true;
		}
	}

	@Override
	protected void tryAgain()
	{
		loadData(LOAD_DATA_FIRST, 0);
	}

	@Override
	protected View setCenterView()
	{
		return View.inflate(this, R.layout.movie_home, null);
	}

	@Override
	public void onBackPressed()
	{
		if (typeChoiceLayout != null
				&& typeChoiceLayout.getVisibility() == View.VISIBLE)
		{
			typeChoiceLayout.setVisibility(View.GONE);
			return;
		}
		super.onBackPressed();
	}

	@Override
	protected void showTypeChoice()
	{
		super.showTypeChoice();
		if (typeChoiceLayout.getVisibility() == View.VISIBLE)
		{
			typeChoiceLayout.setVisibility(View.GONE);
		} else
		{
			typeChoiceLayout.setVisibility(View.VISIBLE);
		}
	}

}
